<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <div id="app">
        <input type="button" value="GET" onclick="get();">
        <input type="button" value="POST" onclick="post();">
    </div>

    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/jsencrypt/3.0.0-rc.1/jsencrypt.min.js"></script>
    <script src="./js/encrypt.js"></script>
    <script src="./js/fetch.js"></script>
    <script>
        const publicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArkDK6QM+edJrQJeuy7VdnCh4CSt7yolgFAPKxLq/5K9lg2BXV1/H1UmhwdD5FBjKDjIxcgus5l0vF/3XYUFcH4MBnR3s/uK26qbEOfSGHaYqxuDKGgOUEpY5pSt5FVdf/awuK2lxog4EMsW30aE0oXj/ULG9grqKUrVbOzkoFX5x4NKcPECII69ydgmmoeMeURueu7vP/kQQcoc1tR12oRpGaD3gfn1L1QUsftPq65JUTgl4W8PObJnMJQ/pnI5hNlWJ3c89+Gka20bvjBGqwaALj2n2HDyLszKq1gtv8B3N7PT7imM/c7+vPpifs3ZtR+wzmm0sDzHa0I3rZEGmBQIDAQAB',
            privateKey = 'MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCuQMrpAz550mtAl67LtV2cKHgJK3vKiWAUA8rEur/kr2WDYFdXX8fVSaHB0PkUGMoOMjFyC6zmXS8X/ddhQVwfgwGdHez+4rbqpsQ59IYdpirG4MoaA5QSljmlK3kVV1/9rC4raXGiDgQyxbfRoTSheP9Qsb2CuopStVs7OSgVfnHg0pw8QIgjr3J2Caah4x5RG567u8/+RBByhzW1HXahGkZoPeB+fUvVBSx+0+rrklROCXhbw85smcwlD+mcjmE2VYndzz34aRrbRu+MEarBoAuPafYcPIuzMqrWC2/wHc3s9PuKYz9zv68+mJ+zdm1H7DOabSwPMdrQjetkQaYFAgMBAAECggEBAIImlugPqjxndABcywwQPQkEG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Fkb4gox2rkya/EK1dUOaDGk5kqruhG1NYkpC6ern2N9qA4n5jFomBqr6ru7nVAV6Td+t53trerkeNakUBTVXN9u6MMxw2zKYXwjd6dpXwS0QswivxHQYFBZRA==';
        const str = 'hello world';
        //使用公钥加密
        const encrypt = new Encrypt();
        encrypt.setPublicKey(publicKey);
        encrypt.setPrivateKey(privateKey);
        /*const encrypted = window.btoa(encrypt.encrypt(str));
        console.log('加密后数据:%o', encrypted);
        //使用私钥解密
        const uncrypted = encrypt.decrypt(window.atob(encrypted));
        console.log('解密后数据:%o', uncrypted);

        console.log(window.btoa(str))
        //转码结果 "aGVsbG8gd29ybGQ="

        console.log(window.atob("aGVsbG8gd29ybGQ="))
        //解码结果 "hello world"*/

        function get() {
            const ajax = new $ajax('/corgi/api/auth/encrypt', function (result) {
                console.log(result)
            }, function (error) {
                console.error(error)
            });
            ajax.setType('get')
                .setEncrypt(true)
                .start();
        }
        
        function post() {
            const ajax = new $ajax('/corgi/api/auth/decrypt', function (result) {
                console.log(result)
            }, function (error) {
                console.error(error)
            });
            ajax.setEncrypt(true)
                .setData({username: 'admin', realname: '管理员', password: '123456'})
                .start();
        }
    </script>
</body>
</html>